<template>
  <div>
    <el-card>
      <el-button type="primary" icon="el-icon-plus" @click="showDialogVisible">添加</el-button>
    <el-table border style="width: 100%" class="mt-10" inline :data="tmList">
      <el-table-column label="序号" width="180" type="index">
      </el-table-column>
      <el-table-column label="品牌名称" prop="tmName">
      </el-table-column>
      <el-table-column  label="品牌LOGO">
        <template #default="{row,$index}">
          <img :src="row.logoUrl" style="width: 130px;height:80px;">
        </template>
     </el-table-column>
        <el-table-column label="操作">
          <template #default="{row,$index}">
            <el-button icon="el-icon-edit" type="warn">编辑</el-button>
            <el-button icon="el-icon-delete" type="danger">删除</el-button>
          </template>
        </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :page-sizes="[3,6,9]"
      layout=" prev, pager, next, jumper, ->, sizes, total"
      :total="total"
      >
    </el-pagination>
    <!-- 点击添加弹出对话框 -->
    <el-dialog
      title="添加品牌"
      :visible.sync="dialogVisible"
      width="30%"
      >
      <!-- :before-close="handleClose" -->
      <el-form>
        <el-form-item label="品牌名称" label-width="70px">
          <el-input></el-input>
        </el-form-item>
        <el-form-item label="品牌LOGO">
          <el-upload
            class="avatar-uploader"
            action="https://jsonplaceholder.typicode.com/posts/"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload">
          <img v-if="imageUrl" :src="imageUrl" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
    </span>
</el-dialog>
    </el-card>
  </div>
</template>
<script>
import {getTrademarkList} from '@/api/trademark'
export default {
  data() {
    return {
      total:0,
      currentPage4:1,
      page:1,
      limit:3,
      tmList:[],
      dialogVisible:false,
      imageUrl: '',
    }
  },
  methods:{
    handleSizeChange(val){
      this.limit = val;
      this.getPage();
    },
    handleCurrentChange(val){
      this.page = val;
      this.getPage();
    },
    handleAvatarSuccess(res, file) {
        this.imageUrl = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
        return isJPG && isLt2M;
      },
    // 点击添加弹出
    showDialogVisible(){
      this.dialogVisible = true;
    },
    //获取品牌商品的数据
    async getPage(){
      try {
        let result = await getTrademarkList(this.page,this.limit); 
        // console.log(result.data);
        this.tmList = result.data.records;
        this.total = result.data.total;
        // console.log(this.tmList);
      } catch (error) {
        console.log(error);
      }
    },
  },
  mounted(){
    this.getPage();
  }

}
</script>

<style scoped>
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>